<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :xs="15" :sm="18" :md="19" :lg="20" :xl="20">
        <!--工具栏-->
        <div class="head-container">
          <div v-if="crud.props.searchToggle">
            <!-- 搜索 -->
            <el-input
              v-model="query.keyword"
              clearable
              size="small"
              placeholder="输入姓名"
              style="width: 200px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
            <rrOperation />
          </div>
          <crudOperation show="" :permission="permission" />
        </div>

        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
          <el-table-column :show-overflow-tooltip="true" prop="realName" label="姓名" />
          <el-table-column prop="userName" label="用户名" />
          <el-table-column label="角色">
            <template slot-scope="scope">
              <span v-if="scope.row.roleSign === 'admin'">管理员</span>
              <span v-if="scope.row.roleSign === 'judge'">评委</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              {{ scope.row.statusInfo }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="270">
            <template slot-scope="scope">
              <el-button class="btn" icon="el-icon-edit" type="primary" size="mini" @click="edit(scope.row)" />
              <!-- <el-button class="btn" :loading="scope.row.del_loading" icon="el-icon-delete" type="danger" size="mini" @click="del(scope.row)" /> -->
              <!-- <el-button class="btn" :loading="scope.row.r_loading" icon="el-icon-" type="success" size="mini" @click="changeStatus(scope.row,1,'r_loading')">{{scope.row.ifRecommend ? '不推荐' : '推荐'}}</el-button> -->
              <!-- <el-button class="btn" :loading="scope.row.s_loading" icon="el-icon-" type="warning" size="mini" @click="changeStatus(scope.row,2,'s_loading')">{{scope.row.ifSelected ? '不精选' : '精选'}}</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import crudAdmin from '@/api/web/admin'
import CRUD, {
  presenter,
  header,
  form,
  crud
} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
export default {
  name: 'Admin',
  components: {
    crudOperation,
    rrOperation,
    udOperation,
    pagination,
    DateRangePicker
  },
  cruds() {
    return CRUD({
      title: '用户',
      url: 'api/admin/list',
      methodStr: 'post',
      dataStr: ['data', 'adminList'],
      optShow: {
        reset: false,
        othoer: true
      },
      crudMethod: { ...crudAdmin }
    })
  },
  mixins: [presenter(), header(), crud()],
  data() {
    // 自定义验证
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
      height: document.documentElement.clientHeight - 180 + 'px;',
      defaultProps: {
        children: 'children',
        label: 'name',
        isLeaf: 'leaf'
      },
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'article:edit'],
        del: ['admin', 'user:del']
      }
    }
  },
  mounted: function() {
    const that = this
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 180 + 'px;'
    }
  },
  methods: {
    getLabels(item) {
      const labels = []
      labels.push(item.labelName)
      if (item.smallLabelList && item.smallLabelList.length) {
        item.smallLabelList.forEach(val => {
          labels.push(val.key)
        })
      }
      return labels.join(',')
    },
    edit(item) {
      this.$router.push('/web/admin/add?' + this.$qs.stringify(item))
    },
    del(item) {
      this.$confirm(`文章删除后不可恢复，确认删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(item, 'del_loading', true)
        crudAdmin.del({ forumId: item.forumId }).then(res => {
          this.$set(item, 'del_loading', false)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.msg
            })
            this.crud.toQuery()
          } else {
            this.$message({
              type: 'error',
              message: res.message
            })
          }
        })
      })
    },
    changeStatus(item, type, loadingStr) {
      let confirmContent = ''
      let confirmStatus = false
      if (type == 1) {
        if (item.ifRecommend) {
          confirmContent = '取消推荐'
        } else {
          confirmContent = '推荐'
        }
        confirmStatus = !item.ifRecommend
      } else {
        if (item.ifSelected) {
          confirmContent = '取消精选'
        } else {
          confirmContent = '精选'
        }
        confirmStatus = !item.ifSelected
      }
      this.$confirm(`是否${confirmContent}该文章`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$set(item, loadingStr, true)
        crudAdmin.changeStatus({ id: item.id, type: type, status: confirmStatus }).then(res => {
          this.$set(item, loadingStr, false)
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.crud.toQuery()
          } else {
            this.$message({
              type: 'arror',
              message: res.message
            })
          }
        }).catch(e => {
          this.$set(item, loadingStr, false)
        })
      })
    },
    // 禁止输入空格
    keydown(e) {
      if (e.keyCode === 32) {
        e.returnValue = false
      }
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .vue-treeselect__control,
  ::v-deep .vue-treeselect__placeholder,
  ::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }

  .n_avatar {
    max-width: 120px;
    max-height: 120px;
  }
  .btn{
    margin: 0;
  }
</style>
